package com.hoshiicloud.fileserver.controller;

import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.fileserver.config.ObsConfig;
import com.hoshiicloud.fileserver.model.CreateBucketRequest;
import com.hoshiicloud.fileserver.service.BucketService;
import com.obs.services.model.ObsBucket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/bucket")
@Api(tags = "存储空间操作")
public class BucketController {
	@Autowired
	private BucketService bucketService;

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ApiOperation(value = "创建一个存储空间", notes = "已存在无法创建,创建成功返回桶静态访问域名", consumes = "application/json")
	public Response createBucket(CreateBucketRequest param) throws Exception {
		if (bucketService.createBucket(param)) {
			String bucketUrl="https://"+param.getName()+"."+ ObsConfig.getDefaultEndpoint();
			return Response.data(bucketUrl);
		} else {
			return Response.failed("操作失败,存储空间已存在");
		}

		// return "test";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ApiOperation(value = "获取已创建的存储空间列表", notes = "", consumes = "application/json")
	public Response<List<ObsBucket>> list() throws Exception {
		return Response.data(bucketService.getBucketList());
	}

	@RequestMapping(value = "/test", method = { RequestMethod.POST, RequestMethod.GET })
	@ApiIgnore
	public String test() throws Exception {
		bucketService.test();
		return "test";
	}
}
